<template>
  <div class="single-image-upload component-img-upload">
    <div class="plugin-images">
      <ul
        v-if="modelValue"
        class="el-upload-list el-upload-list--picture-card"
        @click="elxImgboxHandle"
      >
        <li
          tabindex="0"
          class="el-upload-list__item is-success"
        >
          <img-show
            :src="modelValue"
            :class-list="['el-upload-list__item-thumbnail']"
          />
        </li>
      </ul>

      <div
        v-else
        tabindex="0"
        class="el-upload el-upload--picture-card"
        @click="elxImgboxHandle"
      >
        <el-icon><Plus /></el-icon>
      </div>
    </div>
    <!-- 弹窗, 新增图片 -->
    <elx-imgbox
      v-if="elxImgboxVisible"
      ref="elxImgboxRef"
      @refresh-pic="refreshPic"
    />
    <el-dialog
      v-model="visible"
      :append-to-body="visible"
      :title="$t('prodList.lookOver')"
    >
      <img-show :src="modelValue" />
    </el-dialog>
  </div>
</template>

<script setup>
const props = defineProps({
  modelValue: {
    default: '',
    type: String
  },
  disabled: {
    default: false,
    type: Boolean
  }
})
const emit = defineEmits(['update:modelValue', 'input'])

const elxImgboxVisible = ref(false)
const visible = ref(false)

const elxImgboxRef = ref(null)
// 打开图片选择窗
const elxImgboxHandle = () => {
  if (props.disabled) {
    openImg()
    return false
  }
  elxImgboxVisible.value = true
  nextTick(() => {
    elxImgboxRef.value?.init(1)
  })
}
const openImg = () => {
  visible.value = true
}
const refreshPic = (imagePath) => {
  emit('update:modelValue', imagePath)
  emit('input')
}

</script>

<style lang="scss" scoped>
.component-img-upload {
  :deep(.el-upload) {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    .pic-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 100px;
      height: 100px;
      line-height: 100px;
      text-align: center;
    }
    .pic {
      width: 100px;
      height: 100px;
      display: block;
    }
    &:hover {
      border-color: #409eff;
    }
  }

  :deep(.plugin-images) {
    .el-upload--picture-card {
      width: 100px;
      height: 100px;
      line-height: 112px;
      border-radius: 2px;
    }
    .el-upload-list {
      .el-upload-list__item {
        width: 100px;
        height: 100px;
        border-radius: 2px;
      }
    }
  }
}

</style>
